//
//  TGInformationViewController.m
//  WanCaiC8
//
//  Created by QiuXun on 2018/11/30.
//  Copyright © 2018 tangang. All rights reserved.
//

#import "TGInformationViewController.h"
#import "InformationCell.h"
#import "InformationLeftImageCell.h"
#import "InformationImageCell.h"
#import "ImageTableViewCell.h"

#import "InformationVM.h"
#import "PredictVC.h"

#import "HomeInformationVC.h"

@interface TGInformationViewController ()<UITableViewDelegate,UITableViewDataSource>


@property(nonatomic,strong)UITableView *tableView;


@property(nonatomic,strong)NSMutableArray *dataArr;

@property(nonatomic,strong)NSArray *advertisingArr;





@property(nonatomic,assign)NSInteger  page;



@property(nonatomic,assign)NSInteger  adType;

@end

@implementation TGInformationViewController

-(void)setupRightItem{
    
    UIBarButtonItem *rightItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"nav_icon"] style:UIBarButtonItemStylePlain target:self action:@selector(onclickNav)];
    self.navigationItem.rightBarButtonItem=rightItem;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    [self setupRightItem];
    
    self.page = 1;
    self.dataArr = [NSMutableArray array];
    
    switch (self.type) {
        case TGInformationTypeInformation:
            self.title = @"六合彩彩票资讯";
            self.adType =15;

            [self getLastNewsList];
            
            break;
        case TGInformationTypePicture:
            self.adType =16;

            self.title = @"六合彩最新图库";
            
            [self getPictureList];
            break;
        default:
            break;
    }

    
}

#pragma mark --获取六合彩最新图库
-(void)getPictureList{
    
    WS(weakSelf);
    [[InformationVM manage] getTuNewsListWithPageIndex:self.page PageSize:[defaultPageSize integerValue] Success:^(id returnValue) {
        
        [weakSelf.dataArr addObjectsFromArray:returnValue];
        [weakSelf.tableView reloadData];
        [weakSelf.tableView.header endRefreshing];
        NSArray *arr=returnValue;
        if (arr.count<[defaultPageSize integerValue]) {
            [weakSelf.tableView.footer noticeNoMoreData];
        }else{
            [weakSelf.tableView.footer endRefreshing];
        }
        weakSelf.page+=1;
        
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            [self getAdvertising];
        });
    } Fail:^(NSString *errorMessage, NSError *error) {
        [weakSelf.tableView.footer endRefreshing];
        [weakSelf.tableView.header endRefreshing];
    }];
    

    
}

#pragma mark --获取最新资讯新闻列表
- (void)getLastNewsList{
    
    WS(weakSelf);
    
    //    if (self.newTypeArr.count<=self.selectNewsType) {
    //        [self.tableView.header endRefreshing];
    //        return;
    //    }
    //    [SVProgressHUD show];
    //
    //    NewsTypeModel *model=self.newTypeArr[self.selectNewsType];
    //    WS(weakSelf);
    //    if (model.ShowType==1) {
    //        self.galleryView.hidden=YES;
    //
    //    }
  
    
    [[InformationVM manage] getLastNewsListWithLtype:@"5" Page:self.page PageSize:[defaultPageSize integerValue] NotInLtype:0 Success:^(id returnValue) {
        
        [weakSelf.dataArr addObjectsFromArray:returnValue];
        [weakSelf.tableView reloadData];
        [weakSelf.tableView.header endRefreshing];
        NSArray *arr=returnValue;
        if (arr.count<[defaultPageSize integerValue]) {
            [weakSelf.tableView.footer noticeNoMoreData];
        }else{
            [weakSelf.tableView.footer endRefreshing];
        }
        weakSelf.page+=1;
        
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            [self getAdvertising];
        });
        
       
        
    } Fail:^(NSString *errorMessage, NSError *error) {
        
        
        [weakSelf.tableView.footer endRefreshing];
        [weakSelf.tableView.header endRefreshing];
    }];

}


- (void)backClick{
    
    


    if ([self.saveViewController isMemberOfClass:[HomeInformationVC class]]) {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
    
    [super backClick];
}

#pragma mark -- 获取广告
- (void)getAdvertising{
    WS(weakSelf);
    
    
    
   
    
    [[InformationVM manage] getAdvertWithLocation:@"0" andType:self.adType Success:^(id returnValue) {
        weakSelf.advertisingArr=returnValue;
        [weakSelf insertAdvertising];
    } andFail:^(NSString *errorMessage, NSError *error) {

    }];
}
#pragma mark -- 插入广告
- (void)insertAdvertising{
    NSMutableArray *arr1=[[NSMutableArray alloc]init];
    NSMutableArray *arr2=[[NSMutableArray alloc]init];
    NSMutableArray *arr3=[[NSMutableArray alloc]init];
    NSMutableArray *arr4=[[NSMutableArray alloc]init];
    NSMutableArray *arr5=[[NSMutableArray alloc]init];
    NSMutableArray *arr6=[[NSMutableArray alloc]init];
    NSMutableArray *arr7=[[NSMutableArray alloc]init];

    for (AdvertisingModel *model in self.advertisingArr) {
        if (model.Display) {
            if (model.Layer==1) {
                [arr1 addObject:model];
            }else if (model.Layer==2){
                [arr2 addObject:model];
            }else if (model.Layer==3){
                [arr3 addObject:model];
            }else if (model.Layer==4){
                [arr4 addObject:model];
            }else if (model.Layer==5){
                [arr5 addObject:model];
            }else if (model.Layer==6){
                [arr6 addObject:model];
            }else if (model.Layer==7){
                [arr7 addObject:model];
            }
        }
    }

    
  
    if (self.dataArr.count>7&&arr7.count>0) {
        [self.dataArr insertObjects:arr7 atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(7, arr7.count)]];
    }
    if (self.dataArr.count>6&&arr6.count>0) {
        [self.dataArr insertObjects:arr6 atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(6, arr6.count)]];
    }
    if (self.dataArr.count>5&&arr5.count>0) {
        [self.dataArr insertObjects:arr5 atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(5, arr5.count)]];
    }
    if (self.dataArr.count>5&&arr4.count>0) {
        [self.dataArr insertObjects:arr4 atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(4, arr4.count)]];
    }
    if (self.dataArr.count>4&&arr3.count>0) {
        [self.dataArr insertObjects:arr3 atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(3, arr3.count)]];
    }
    if (self.dataArr.count>2&&arr2.count>0) {
        [self.dataArr insertObjects:arr2 atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(2, arr2.count)]];
    }
    if (self.dataArr.count>1&&arr1.count>0) {
        [self.dataArr insertObjects:arr1 atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, arr1.count)]];
    }
    
   
    [self.tableView reloadData];
}



- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.hidesBottomBarWhenPushed = NO;
    
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.dataArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.dataArr[indexPath.row] isKindOfClass:[NewsModel class]]) {
        NewsModel *model=self.dataArr[indexPath.row];
        if (model.ThumbStyle==0) {
            InformationCell *cell=[tableView dequeueReusableCellWithIdentifier:@"InformationCell" forIndexPath:indexPath];
            [cell setModel:model];
            return cell;
        }else if (model.ThumbStyle==1){
            InformationLeftImageCell *cell=[tableView dequeueReusableCellWithIdentifier:@"InformationLeftImageCell" forIndexPath:indexPath];
            [cell setModel:model];
            return cell;
        }else{
            InformationImageCell *cell=[tableView dequeueReusableCellWithIdentifier:@"InformationImageCell" forIndexPath:indexPath];
            [cell setModel:model];
            return cell;
        }
    }else{
        AdvertisingModel *model=self.dataArr[indexPath.row];
        if (model.ThumbStyle==0) {
            InformationCell *cell=[tableView dequeueReusableCellWithIdentifier:@"InformationCell" forIndexPath:indexPath];
            [cell setAdModel:model];
            return cell;
        }else if (model.ThumbStyle==1){
            InformationLeftImageCell *cell=[tableView dequeueReusableCellWithIdentifier:@"InformationLeftImageCell" forIndexPath:indexPath];
            [cell setAdModel:model];
            return cell;
        }else if (model.ThumbStyle==4){
            ImageTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"ImageTableViewCell" forIndexPath:indexPath];
            if (model.ThumbList.count>0) {
                [cell.imageV sd_setImageWithURL:model.ThumbList[0] placeholderImage:[UIImage imageNamed:@"big_loding"]];
            }else{
                cell.imageV.image=[UIImage imageNamed:@"big_loding"];
            }
            
            return cell;
        }else{
            InformationImageCell *cell=[tableView dequeueReusableCellWithIdentifier:@"InformationImageCell" forIndexPath:indexPath];
            [cell setAdModel:model];
            return cell;
        }
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if ([self.dataArr[indexPath.row] isKindOfClass:[NewsModel class]]) {
//        NewsLottryTypeModel *newsModel=self.lotteryArr[self.selectNewsType];
        
        NewsModel *model=self.dataArr[indexPath.row];
//        model.TypeId=newsModel.Id;
        
        PredictVC  *vc=[[PredictVC alloc]init];
        vc.model=model;
        vc.newsType = model.TypeId;
        vc.saveViewController=self;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        AdvertisingModel *model=self.dataArr[indexPath.row];

        if ([model.Title containsString:@"】"]) {

            UIPasteboard *copy = [UIPasteboard generalPasteboard];
            NSRange startRange = [model.Title rangeOfString:@"【"];
            NSRange endRange = [model.Title rangeOfString:@"】"];
            NSRange range = NSMakeRange(startRange.location + startRange.length, endRange.location - startRange.location - startRange.length);
            NSString *result = [model.Title substringWithRange:range];

            [copy setString:result];

            [SVProgressHUD showSuccessWithStatus:@"复制成功"];

            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [SVProgressHUD dismiss];
                [[UIApplication sharedApplication]openURL:[NSURL URLWithString: @"weixin://"]];
            });

        }else{

            if (model.TransferUrl.length<1) {
                return;
            }
            [[UIApplication sharedApplication]openURL:[NSURL URLWithString: model.TransferUrl]];

        }
    }
}




- (UITableView *)tableView{
    if (!_tableView) {
        WS(weakSelf);
        _tableView=[[UITableView alloc]init];
        _tableView.dataSource=self;
        _tableView.delegate=self;
        _tableView.backgroundColor=DEFAULTBGCOLOR;
        [_tableView registerClass:[InformationCell class] forCellReuseIdentifier:@"InformationCell"];
        [_tableView registerClass:[InformationLeftImageCell class] forCellReuseIdentifier:@"InformationLeftImageCell"];
        [_tableView registerClass:[InformationImageCell class] forCellReuseIdentifier:@"InformationImageCell"];
        [_tableView registerClass:[ImageTableViewCell class] forCellReuseIdentifier:@"ImageTableViewCell"];
        
        _tableView.estimatedRowHeight=150.0f;
        _tableView.rowHeight=UITableViewAutomaticDimension;
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        _tableView.header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
            weakSelf.page=1;
            [weakSelf.dataArr removeAllObjects];
            [weakSelf.tableView reloadData];
            [weakSelf getLastNewsList];
        }];
        _tableView.footer=[MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            [weakSelf getLastNewsList];
        }];
        [self.view addSubview:_tableView];
        
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(weakSelf.view);
        }];

    }
    return _tableView;
}

@end
